﻿//using System;
using System.Web.SessionState;
using MasApp;

namespace MasApp.BLL
{
    /// <summary>
    /// session 封装操作类
    /// </summary>
    public static class SessUtil
    {
        public static HttpSessionState Session = null;
        /// <summary>
        /// 验证码键名
        /// </summary>
        public readonly static string validate_code_name = "validateCode";
        /// <summary>
        /// 用户信息键名
        /// </summary>
        public readonly static string user_key_name = "username";

        /// <summary>
        /// 判断是否存在session
        /// </summary>
        /// <param name="session"></param>
        /// <returns>bool</returns>
        public static bool existSessionKey(HttpSessionState session, string key)
        {
            SessUtil.Session = session;
            return SessUtil.Session[key] == null ? false : true;
        }
        /// <summary>
        /// 判断是否存在session
        /// </summary>
        /// <returns>bool</returns>
        public static bool existSessionKey(string key)
        {
            return SessUtil.Session == null || SessUtil.Session[key] == null ? false : true;
        }
        /// <summary>
        /// 设置验证码
        /// </summary>
        /// <param name="session"></param>
        /// <param name="value"></param>
        public static void setValidateKey(HttpSessionState session, string value)
        {
            SessUtil.Session = session;
            if (SessUtil.Session != null)
            {
                SessUtil.Session.Add(SessUtil.validate_code_name, value);
            }
        }
        /// <summary>
        /// 设置验证码
        /// </summary>
        /// <param name="session"></param>
        /// <param name="value"></param>
        public static void setValidateKey(string value)
        {
            if (SessUtil.Session != null)
            {
                SessUtil.Session.Add(SessUtil.validate_code_name, value);
            }
        }
        /// <summary>
        /// 返回验证码
        /// </summary>
        /// <param name="session"></param>
        /// <returns>验证码</returns>
        public static string getValidateKey(HttpSessionState session)
        {
            SessUtil.Session = session;
            if (SessUtil.Session == null)
            {
                return "aaa";
            }
            else if (SessUtil.Session[SessUtil.validate_code_name] == null)
            {
                foreach (object o in SessUtil.Session)
                {
                    System.Web.HttpContext.Current.Response.Write(o.ToString() + ":" + SessUtil.Session[o.ToString()]);
                }
                return SessUtil.validate_code_name + "--" + SessUtil.Session.Count;
            }
            return SessUtil.Session[SessUtil.validate_code_name] == null ? "" : SessUtil.Session[SessUtil.validate_code_name].ToString();
        }
        /// <summary>
        /// 返回验证码
        /// </summary>
        /// <param name="session"></param>
        /// <returns>验证码</returns>
        public static string getValidateKey()
        {
            return SessUtil.Session == null || SessUtil.Session[SessUtil.validate_code_name] == null ? System.String.Empty : SessUtil.Session[SessUtil.validate_code_name].ToString();
        }
    }
}